<?php

namespace App\Http\Service;

use App\Models\Commentvote;
use App\Models\Comment;

use App\Http\Service\UserService;
use App\Http\Service\BaseService;
use App\Http\Service\CommentService;
use Illuminate\Support\Facades\DB;
class CommentvoteService  extends BaseService
{
    private $model;

    public function __construct(Commentvote $model,UserService $userService,CommentService $commentService,Comment $comment)
    {
        parent::__construct();
        $this->model = $model;
        $this->userService=$userService;
        $this->commentService=$commentService;
        $this->comment=$comment;
       
    }
   
    //点赞、踩
    public function addData($request,$data=[])
    {
        $where=[
            'comment_id'=>$data['comment_id'],
            'user_id'=>$data['user_id'],
        ];
        $data['is_deleted']=2;
        $_vote_type=$data['vote_type'];
        $first=$this->model->where($where)->first();

        if($first){
            $vote_type=$first['vote_type'];
            # 如果一样就是取消
            if($vote_type==$_vote_type){
                $data['vote_type']='2';
            }

            $data['update_time']=date("Y-m-d h:i:s");
            $this->model->where($where)->update($data);
        }else{
            $data['create_time']=date("Y-m-d h:i:s");
            $data['update_time']=date("Y-m-d h:i:s");
            $this->model->where($where)->insertGetId($data);
        }

        #更新commit评论表
        $this->commentService->updateLikesDislikes($data['comment_id']);
        
        #获取评论次数
        $comment=$this->comment->where(['id'=>$data['comment_id']])->first();
        $result=[];
        $result=[
            'like_count'=>$comment['like_count'],
            'dislike_count'=>$comment['dislike_count'],
        ];
        return $result;
              
    }
    
}
